package com.zxiw.zxedu.recyclerview.adapter

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.Interpolator


/**
 * @author zhangxiaoming
 * @date 2023/8/18
 * @description
 */
class AnimationUtil {
    var mAnimationType = AnimationType.ALPHA
    var mCustomAnimator: Animator? = null
    var mTargetView: View? = null
    var mInterpolator: Interpolator? = null
    var mDuration: Long = 300
    fun start() {
        if (mCustomAnimator != null) {
            mCustomAnimator?.start()
        } else if (null == mTargetView) {
            throw IllegalArgumentException("You must set a target view!")
        } else {
            startAnimation(mAnimationType)
        }
    }

    private fun startAnimation(animationType: AnimationType) {
        val animatorSet = AnimatorSet()
        when (animationType) {
            AnimationType.ALPHA -> animatorSet.play(
                ObjectAnimator.ofFloat(
                    mTargetView,
                    "alpha",
                    0.7f,
                    1f
                )
            )
            AnimationType.SCALE -> animatorSet.playTogether(
                ObjectAnimator.ofFloat(
                    mTargetView,
                    "scaleX",
                    0.6f,
                    1f
                )
            )
            AnimationType.SLIDE_FROM_BOTTOM -> animatorSet.play(
                ObjectAnimator.ofFloat(
                    mTargetView,
                    "translationY",
                    mTargetView?.measuredHeight?.toFloat() ?: 0f,
                    0f
                )
            )
            AnimationType.SLIDE_FROM_LEFT -> animatorSet.play(
                ObjectAnimator.ofFloat(
                    mTargetView,
                    "translationX",
                    -(mTargetView?.rootView?.width?.toFloat() ?: 0f),
                    0f
                )
            )
            AnimationType.SLIDE_FROM_RIGHT -> animatorSet.play(
                ObjectAnimator.ofFloat(
                    mTargetView,
                    "translationX",
                    mTargetView?.rootView?.width?.toFloat() ?: 0f,
                    0f
                )
            )
        }
        if (mInterpolator != null) {
            animatorSet.interpolator = mInterpolator
        }
        animatorSet.duration = mDuration
        animatorSet.start()
    }
}